//
//  AiGirlGlobalTool.swift
//  AIGirl
//
//  Created by Rae on 2024/1/8.
//

import UIKit

let GlobalTool = AIGirlGlobalTool.shared

class AIGirlGlobalTool: NSObject, UorInXNetworkProtocol, ObservableObject {

    static let shared = AIGirlGlobalTool()
    
    public var bannerModel: [AIGirlBannerItemModel] =  [AIGirlBannerItemModel]()
    /// 用户Token
    var userToken: String {
        get {
//            UserDefaults.standard.string(forKey: "AIGirl_UserToken") ?? ""
            UserDefaultsManager.getString(forKey: .token) ?? ""
        } set {
            UserDefaultsManager.setString(newValue, forKey: .token)
            // 设置 token
            RequestParameterConfig.shared.requestToken = newValue
        }
    }
    
    /// 用户模型
     var userModel: AIGirlUserModel? {
        didSet {
            guard let userModel else {
//                UserDefaults.standard.removeObject(forKey: "AIGirl_UserToken")
                UserDefaultsManager.removeForKey(forKey: .token)
                return
            }
            GlobalTool.userToken = userModel.token
            
//            DispatchQueue.main.async {
//                NotificationCenter.default.post(name: .changeUserInfos, object: nil)
//            }
            
        }
    }
    
}

//MARK: - 刷新用户信息
extension AIGirlGlobalTool {
    
    func refreshUserInfo(_ isFromWeb: Bool = false,success: (() -> Void)? = nil) {
        let historyMoney = userModel?.money ?? 0
        let historyVIP = userModel?.isVip ?? "0"
        
        request.start(.RefreshUserModel, type: AIGirlUserModel.self) { [weak self] result in
            if result.isSuccessful, let model = result.model {
//                GlobalTool.userModel = result.model
                self?.userModel = model
                if model.isVip.intValue == 0 {
                    UserDefaultsManager.setString("", forKey: .VIPStartTime)
                    UserDefaultsManager.setString("", forKey: .VIPEndTime)
                }
                if historyMoney != model.money {
                    NotificationCenter.default.post(name: .userBalanceChanged, object: nil)
                }
                if historyVIP != model.isVip {
                    NotificationCenter.default.post(name: .updateVIPState, object: nil)
                }
                success?()
            }
        }
    }
    
    func getBannerList(){
        
        request.start(.QueryFirstRecharge, type: AIGirlBannerModel.self) { result in
            
            if result.isSuccessful, let model = result.model {
                self.bannerModel.removeAll()
                if model.bannerList.count > 0 {
                    for md in model.bannerList {
                        if md.imageUrl.count > 0 {
                            self.bannerModel.append(md)
                        }
                    }
                }
            }
        }
    }
    
    func hiddenLogin(){
        
        request.start(.HiddenLogin,type: AIGirlUserModel.self) { result in
            if result.isSuccessful, let model = result.model {
                GlobalTool.userModel = model
            }
        }
    }
    
    func showVipInfoPage(dismissClosure: (() -> Void)? = nil, buyUpdateClosure: (() -> Void)? = nil) {
        let vipController = AIGirlSubscribeViewController()
        vipController.dismissClosure = dismissClosure
        vipController.buyUpdateClosure = buyUpdateClosure
        let controller = AIGirlNavViewController(rootViewController: vipController)
        controller.modalPresentationStyle = .overFullScreen
        UIApplication.topController()?.present(controller, animated: true)
    }
    
    func showAlreadyVipInfoPage() {
        let vipController = AIGirlSubscribeAlreadyViewController()
        let controller = AIGirlNavViewController(rootViewController: vipController)
        controller.modalPresentationStyle = .overFullScreen
        UIApplication.topController()?.present(controller, animated: true)
    }
    
//    private func getVIPInfo(){
//        
//        request.start(.VipInfo,type: AIGirlVIPModel.self) { result in
//            
//            if result.isSuccessful, let model = result.model {
//                
//                self.userModel?.becomeVIPTime = model.createTime.toTimeStr()
//                self.userModel?.expirationVIPTime = model.expirationTime.toTimeStr()
//            }
//        }
//    }
    
}
